package com.xxy.crm.controller;

import com.xxy.crm.base.BaseController;
import com.xxy.crm.base.ResultInfo;
import com.xxy.crm.query.CustomerQuery;
import com.xxy.crm.service.CustomerLossService;
import com.xxy.crm.service.CustomerOrderService;
import com.xxy.crm.service.CustomerService;
import com.xxy.crm.vo.Customer;
import com.xxy.crm.vo.CustomerLoss;
import com.xxy.crm.vo.CustomerOrder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.management.relation.RelationSupport;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("customer")
public class CustomerController extends BaseController {
    @Resource
    private CustomerService customerService;

    /**
     * 分页查询显示
     * @param customerQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> queryCustomerByParams(CustomerQuery customerQuery){
        return customerService.queryCustomerByParams(customerQuery);
    }

    /**
     * 显示客户信息管理页面
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "customer/customer";
    }

    /**
     * 添加按钮页面跳转
     * @return
     */
    @RequestMapping("toAddOrUpdateCustomerPage")
    public String toAddOrUpdateCustomerPage(Integer id, HttpServletRequest request){
        if (id != null) {
            Customer customer = customerService.selectByPrimaryKey(id);
            request.setAttribute("customer",customer);
        }
        return "customer/add_update";
    }

    /**
     * 新增用户信息
     * @param customer
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addCustomer(Customer customer){
        customerService.addCustomer(customer);
        return success("客户信息添加成功");
    }

    /**
     * 更新用户信息
     * @param customer
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateCustomer(Customer customer){
        customerService.updateCustomer(customer);
        return success("客户信息更新成功");
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomer(Integer id){
        customerService.updateById(id);
        return success("删除成功");
    }

    /**
     * 订单信息页面展示
     * @param customerId
     * @return
     */
    @RequestMapping("toCustomerOrderPage")
    public String toCustomerOrderPage(Integer customerId,HttpServletRequest request){
        Customer customer = customerService.selectByPrimaryKey(customerId);
        request.setAttribute("customer",customer);
        return "customer/customer_order";
    }

}
